package com.wanda.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.mail.Session;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wanda.model.JsonModelGeneral;
import com.wanda.model.ProjectModel;
import com.wanda.model.User_Role_OrganizationModel;
import com.wanda.model.Wd_UserModel;
import com.wanda.service.ProjectService;
import com.wanda.service.Wd_UserService;
import com.wanda.util.DateFormat;

@Controller
@RequestMapping("qt/ht/project") 
public class ProjectController {

	@Autowired
	private ProjectService projectService;
	
	@Resource
	private Wd_UserService wd_UserService;
	
	// 显示项目信息ModelAndView
	@RequestMapping(value = "/selectAll.do")
	public ModelAndView showAll(HttpServletRequest request ,String munber,HttpSession session) {
		ModelAndView model = new ModelAndView();
		List<ProjectModel> listp=null;
		//List<ProjectModel> listpjm= new ArrayList<ProjectModel>();
		System.err.println("user:user");
		Wd_UserModel user = (Wd_UserModel) request.getSession().getAttribute("bean");
		System.err.println("user:"+user.getTel());
		try { 
			if(user.getTel().equals("admin")){
				listp = projectService.selectAll();
				System.err.println("listp:"+listp);
				model.setViewName("/ht/listProject.jsp");
				model.addObject("projectList", listp);
				//request.getSession().setAttribute("pross", listp);
			}else{
				listp = projectService.selectAlll(user.getTel());
				System.err.println("listppp:"+listp);
				model.setViewName("/ht/listProject.jsp");
				model.addObject("projectList", listp);
				//request.getSession().setAttribute("pross", listp);
			}
/*		
		if(!(munber==null)){
			System.err.println("1");
			if (user != null) {
				System.err.println("2");
				if (!(user.getTel().equals("admin"))) {
					System.err.println("3");
					for (ProjectModel p : listp) {
						System.err.println("4");
						if(p.getTel()!=null){
							System.err.println("5");
							if(p.getTel().equals(user.getTel())){
								System.err.println("6");
								listpjm.add(p);
							}	
						}
					}
					listp=listpjm;
				}
			}
			request.getSession().setAttribute("projectpross", listp);
			System.out.println("进入项目查询"+munber);
			model.setViewName("/ht/addProject.jsp");
			model.addObject("projectList1", listp);
		}*/
		//System.err.println(listp.size());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return model;

	}

	
	
/*	@RequestMapping(value = "/selectAllByAdd.do")
	public ModelAndView showAddAll(HttpServletRequest request ,String munber) {
		ModelAndView model = new ModelAndView();
		List<ProjectModel> listp=null;
		List<ProjectModel> listpjm= new ArrayList<ProjectModel>();
		try {
	       listp = projectService.selectAll();
			Wd_UserModel user = (Wd_UserModel) request.getSession().getAttribute("bean");
		if (!(user.getTel().equals("admin")||user.getTel()==("admin"))) {
			
					for (ProjectModel p : listp) {
						if(p.getTel()!=null){
							if(p.getTel().equals(user.getTel())){
								listpjm.add(p);
								System.out.println("进入项目11111111111111111111111查询"+p.getTel());
							}	
						}
					}
					listp=listpjm;
				
		}
		request.getSession().setAttribute("projectpross", listp);
		
		model.setViewName("/ht/addProject.jsp");
		model.addObject("projectList1", listp);
		System.err.println(listp.size());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return model;

	}*/
	
	@RequestMapping(value = "/selectAllByAdd.do")
	public ModelAndView showAddAll(HttpServletRequest request) {
		ModelAndView model = new ModelAndView();
		//List<User_Role_OrganizationModel> listp=null;
		List<User_Role_OrganizationModel> abc=null;
		try{
			abc = wd_UserService.selectuserAll();
			System.err.println("listppp:"+abc);
		}catch (Exception ex) {
			ex.printStackTrace();
		}
		model.setViewName("/ht/addProject.jsp");
		model.addObject("projectList", abc);
		return model;
		
	}
	
	// 显示项目信息JsonModel
	@RequestMapping(value = "/selectAll1.do")
	public @ResponseBody JsonModelGeneral selectAll() {
		JsonModelGeneral JsonModel = new JsonModelGeneral();
		List<ProjectModel> projects = null;
		try {
			projects = projectService.selectAll();
			JsonModel.setRows(projects);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return JsonModel;

	}

	// 遍历楼栋（单元，房间）表里的项目名字
	@RequestMapping(value = "/selectAlls.do")
	public @ResponseBody Map<String, Object> showAlls(String id) {

		Map<String, Object> map = new HashMap<String, Object>();

		System.err.println("进入项目查询");
		List<ProjectModel> pros = null;
		pros = projectService.selectAll();

		map.put("msg", pros);
		return map;
	}

	@RequestMapping(value = "/add.do")
	public void add(HttpServletRequest request,HttpServletResponse response) {
		
		Wd_UserModel user = (Wd_UserModel) request.getSession().getAttribute("bean");
		List<ProjectModel> cp = (List<ProjectModel>) projectService.selectAlll(user.getTel());
		
		System.err.println("添加记录达到上限，以不能再添加"+cp.size());
	    if(cp.size()>=3){

			try {
				 PrintWriter out = response.getWriter();
				 response.setContentType("text/html;charset=UTF-8"); 
			        //返回html页面  
				    out.println("<html>");  
				    out.println("<head>");     
				    out.println("<title></title>");      
				    out.println("</head>");    
				    out.println("<body>");   
				    out.println("<script>alert('添加项目次数达到上限');</script>");
				    out.println("<script>window.location.href='http://localhost:8080/wanda_java2/qt/ht/project/selectAll.do'</script>");
				    out.println("</body>");    
				    out.println("</html>"); 
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    }else{
	    	try {
				response.sendRedirect("http://localhost:8080/wanda_java2/qt/ht/project/selectAllByAdd.do");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}   
	    }
		return;
	}
	
	// 添加项目
	@RequestMapping(value = "/addProject.do")
	public @ResponseBody Map<String, Object> add(String project_name, String start_time,
			String end_time,String create_user_id, String remark, String delete_state,
			String state,String product_type,HttpServletRequest request,
			String create_time,String tel) {
		
		ProjectModel model = new ProjectModel();
		Wd_UserModel user = (Wd_UserModel) request.getSession().getAttribute("bean");
		
		System.out.println("进入项目添加");
		model.setProject_name(project_name);
		model.setStart_time(start_time);
		model.setEnd_time(end_time);
		model.setProduct_type(product_type);
		model.setState(state);
		model.setRemark(remark);
		model.setDelete_state("0");
	    model.setCreate_time(DateFormat.getNowDate());
	    
	    System.err.println("admin0000000000000:"+(!("admin".equals(user.getTel()))));
	    
	    if (!("admin".equals(user.getTel()))) {
	    	model.setTel(user.getTel());
	    	 System.err.println("tel11111111:"+user.getTel());
	    	projectService.insertProject(model);
	    	Map<String, Object> map = new HashMap<String, Object>();
			System.out.println("添加成功");
			map.put("msg", "成功");

			return map;
	    }else{
	    	model.setTel(create_user_id);
	    	 System.err.println("tel2222222222:"+create_user_id);
	    	projectService.insertProject(model);
	    	Map<String, Object> map = new HashMap<String, Object>();
			System.out.println("添加成功");
			map.put("msg", "成功");

			return map;
	    }
	}

	// 修改
	@RequestMapping(value = "/updateProject.do")
	public String update(ProjectModel model) {

		System.out.println("进入修改");
		model.setUpdate_time(DateFormat.getNowDate());

		projectService.updateProject(model);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("projectList", map);
		return "redirect:/qt/ht/project/selectAll.do";
	}

	// 软删除一条记录
	@RequestMapping(value = "/deleteProject.do")
	public String delete(ProjectModel model,HttpServletRequest request) {

		Wd_UserModel user = (Wd_UserModel) request.getSession().getAttribute("bean");
		System.out.println("进入删除");
		model.setDelete_time(DateFormat.getNowDate());
		model.setProject_id(model.getProject_id());
        model.setDelete_user(user.getTel());
		System.out.println("user.gettel"+model.getDelete_user());
		
		int ids = projectService.deleteProject(model);

		Map<String, Object> map = new HashMap<String, Object>();
		System.out.println("删除成功");
		map.put("msg", ids);
		return "redirect:/qt/ht/project/selectAll.do";

	}

	// 查询某条数据
	@RequestMapping(value = "/selectById.do")
	public ModelAndView selectById(String id,HttpServletRequest request) {
		
		List<User_Role_OrganizationModel> wd_User = null;
		Wd_UserModel user = (Wd_UserModel) request.getSession().getAttribute("bean");
		ModelAndView model = new ModelAndView();
		System.out.println("进入查询单条" + id);
		ProjectModel pros = null;
		if("admin".equals(user.getTel())){
			pros = projectService.selectById(id);
			wd_User = wd_UserService.selectuserAll();
			model.setViewName("/ht/updateProject.jsp");
			model.addObject("projectById", pros);
			request.getSession().setAttribute("projectBy",wd_User);
		}
		try {
			pros = projectService.selectById(id);
			model.setViewName("/ht/updateProject.jsp");
			model.addObject("projectById", pros);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return model;

	}

}